package me.android.library.ui.impl;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;

import lombok.Getter;
import lombok.Setter;
import me.android.library.common.enums.ConnectivityMode;
import me.android.library.common.event.ConnectionModeChangedEvent;
import me.android.library.ui.api.Page;
import me.android.library.utils.android.ContextUtil;
import me.android.library.utils.android.MemoryUtil;
import me.android.library.utils.android.SoftInputUtil;
import mylab.base.foundation.event.AsyncEventSupporter;

public abstract class AbstractPage extends Fragment implements Page, AsyncEventSupporter {

    @Getter
    @Setter
    protected String pageKey;

    @Getter
    @Setter
    protected String pageTitle;
    protected Resources resources;
    protected View rootView;
    protected Context cx;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AsyncEventSupporter.super.registerEventListener();
        resources = ContextUtil.getResources();
        cx = getContext();
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rootView = view;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        AsyncEventSupporter.super.unregisterEventListener();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (rootView != null) {
            MemoryUtil.disposeView(rootView);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        SoftInputUtil.hide(requireActivity());
        if (rootView != null) {
            rootView.setOnTouchListener(null);
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        if (rootView != null) {
            rootView.setOnTouchListener(new OnTouchListener() {

                @SuppressLint("ClickableViewAccessibility")
                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    return true;
                }
            });
        }
    }


    @Override
    public void onPageActivated() {

    }

    @Override
    public void onPageInActivated() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Subscribe
    @AllowConcurrentEvents
    public void onConnectionModeChangedEvent(ConnectionModeChangedEvent event) {
        onConnectionModeChanged(event.getContent());
    }

    protected void onConnectionModeChanged(ConnectivityMode mode) {
    }

}
